package net.yters.model.agents;

import java.io.IOException;
import java.util.ArrayList;

import net.yters.model.ModelState;
import net.yters.model.agents.actuators.MoveActuator;
import net.yters.model.agents.actuators.ShootActuator;
import net.yters.model.agents.actuators.TurnLeftActuator;
import net.yters.model.agents.actuators.TurnRightActuator;
import net.yters.model.agents.actuators.sensors.ClosestBulletAgentSensor;
import net.yters.model.agents.actuators.sensors.ClosestGoalAgentSensor;
import net.yters.util.Box;

public abstract class TextAgent extends DeciderAgent {
	private static final long serialVersionUID = 5189645199607994027L;

	public TextAgent() {
		super();

		f_direction.clear();
		f_direction.add(new Box(new Double(1.0)));
		f_direction.add(new Box(new Double(0.0)));

		this.init();
	}

	public TextAgent(TextAgent agent) throws IOException, ClassNotFoundException {
		super(agent);
	}
	
	void init() {
		f_hp.f_value = new Double(1.0);

		addSensorAgent(new ClosestGoalAgentSensor());
		addSensorAgent(new ClosestBulletAgentSensor());

		addActuatorAgent(new MoveActuator(1.0));
		addActuatorAgent(new TurnRightActuator()); // 90 degrees clockwise
		addActuatorAgent(new TurnLeftActuator()); // 90 degrees counterclockwise
		addActuatorAgent(new ShootActuator());

		for(Box b : f_position) {
			addBoxedState("pos_" + f_position.lastIndexOf(b), b);
		}

		for(Box b : f_direction) {
			addBoxedState("dir_" + f_direction.lastIndexOf(b), b);
		}

		addBoxedState("choice", f_choice); 
		f_input.put("choice", f_choice);

		for(Agent a : f_actuators) { // TODO HACK Give each actuator an id for easy selection.
			a.f_state.put("id", new Box(new Double(f_actuators.lastIndexOf(a))));
		}
	}

	public TextAgent(ModelState model) {
		super(model);

		ArrayList<Double> dimensions = f_model.f_env.getDimensions();

		// Randomly place agent
		for(Double v : dimensions) {
			f_position.add(new Box((double) Math.round((v - 1) * Math.random())));
		}

		while(f_model.f_env.getContent(net.yters.util.Util.disemBoxArray(f_position)) != 0) {
			f_position.clear();
			for(Double v : dimensions) {
				f_position.add(new Box((double) Math.round((v - 1) * Math.random())));
			}
		}

		// Set initial direction
		f_direction.add(new Box(new Double(1.0)));
		f_direction.add(new Box(new Double(0.0)));

		// Initialize everything else
		this.init();
	}
}
